package jsp.servlet;

import java.io.IOException;
import java.util.GregorianCalendar;
import java.util.Map;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jpa.entities.Employee;
import jsp.classes.Session;

/**
 * @author Remco Haszing
 * @author Aymeric Blok
 */
public class NewUser extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            Session session = (Session) request.getSession().getAttribute("user");
            String username = null;
            String firstname = null;
            String lastname = null;
            String middlename = null;
            GregorianCalendar birthdate = null;
            GregorianCalendar memdate = null;
            GregorianCalendar termdate = null;
            String town = null;
            String postalcode = null;
            String country = null;
            long socialsecurity = -1;
            String[] phonenumber = null;
            String[] emailaddress = null;
            String[] webaddress = null;
            String password = null;
            for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
                String k = entry.getKey();
                String[] v = entry.getValue();
                if (k.equals("username")) {
                    username = v[0];
                } else if (k.equals("firstname")) {
                    firstname = v[0];
                } else if (k.equals("lastname")) {
                    lastname = v[0];
                } else if (k.equals("middlename")) {
                    middlename = v[0];
                } else if (k.equals("birthdate")) {
                    String[] d = v[0].split("-");
                    birthdate = new GregorianCalendar(Integer.parseInt(d[0]), Integer.parseInt(d[1]) - 1, Integer.parseInt(d[2]));
                } else if (k.equals("memdate")) {
                    String[] d = v[0].split("-");
                    memdate = new GregorianCalendar(Integer.parseInt(d[0]), Integer.parseInt(d[1]) - 1, Integer.parseInt(d[2]));
                } else if (k.equals("termdate")) {
                    String[] d = v[0].split("-");
                    termdate = new GregorianCalendar(Integer.parseInt(d[0]), Integer.parseInt(d[1]) - 1, Integer.parseInt(d[2]));
                } else if (k.equals("town")) {
                    town = v[0];
                } else if (k.equals("postalcode")) {
                    postalcode = v[0];
                } else if (k.equals("country")) {
                    country = v[0];
                } else if (k.equals("socialsecurity")) {
                    socialsecurity = Long.parseLong(v[0]);
                } else if (k.equals("phonenumber")) {
                    phonenumber = v;
                } else if (k.equals("emailaddress")) {
                    emailaddress = v;
                } else if (k.equals("webaddress")) {
                    webaddress = v;
                } else if (k.equals("password")) {
                    password = v[0];
                }
            }
            session.getJPAConnection().create(new Employee(username, lastname, firstname, middlename, birthdate, memdate, termdate, town, postalcode, country, socialsecurity, phonenumber, emailaddress, webaddress, password, false));
            response.getWriter().print("true");
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().print("false");
        }
    }
}
